<?php
class vb6 {
  
  const zip_mini_url = 'http://hdforall.googlecode.com/files/scripts_mini.zip';
  const version1_url = 'http://hddlinks.googlecode.com/hg/version.txt';
  const version2_url = 'http://hdforall.uphero.com/version.srt';
  const version1_mini_url = 'http://hdforall.googlecode.com/files/version_mini.txt';
  const version_local = '/usr/local/etc/www/cgi-bin/scripts/version.txt';
  const log_file = '/usr/local/etc/.vb6upd';
  const online_media = '/usr/local/etc/.online_media';
  const scripts_folder = '/usr/local/etc/www/cgi-bin/scripts';
  const tmp_zip = 'tmp_file.zip';
  const scripts_location = '/usr/local/etc/www/cgi-bin/';
  const match_log_content = '~class="logmessage"\s+><a onclick="cancelBubble=true"\s+href="(?:.+?)">(.+?)<span class="extra">~is';
  const match_log_date = '~<td onclick="(?:.+?)"\s*title="(.+?)"\s+nowrap="nowrap"~is';
  const log_url = 'http://code.google.com/p/hddlinks/source/list';
  const store_version = '/usr/local/etc/version.txt';
  const firmware_updates = '~<td onclick="(?:.+?)r=(.+?)\'\)"\s+class="logcontainer"~';
  const rss = '<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">

<bookmark>OnlineScriptsUpdate</bookmark>

<onEnter>
	pageTitle = getPageInfo("pageTitle");
	print("page title is ", pageTitle);
	infoStr = "";
	SetScreenSaverStatus("no");
	setRefreshTime(5);
</onEnter>

<onExit>
	SetScreenSaverStatus("yes");
	setRefreshTime(-1);
</onExit>

<onRefresh>
	redrawDisplay();
	infoStr = readStringFromFile("/usr/local/etc/.vb6upd");
	print("infoStr:",infoStr);
	setRefreshTime(500);
	prompt = "$[OK]";
</onRefresh>
<mediaDisplay
	name=onePartView
	itemPerPage=1
	viewAreaXPC=9.3
	viewAreaYPC=5.3
	viewAreaWidthPC=80.3
	viewAreaHeightPC=90.5

	itemXPC=8.8
	itemYPC=70.0
	itemWidthPC=83.5
	itemHeightPC=12.1
	itemImageXPC=8.8
	itemImageYPC=70.0
	itemImageWidthPC=83.5
	itemImageHeightPC=12.1

	imageFocus=""

	sideColorRight=-1:-1:-1
	sideColorLeft=-1:-1:-1
	sideColorTop=-1:-1:-1
	sideColorBottom=-1:-1:-1
	backgroundColor=-1:-1:-1
	focusBorderColor=-1:-1:-1
	unFocusBorderColor=-1:-1:-1
	itemBackgroundColor=-1:-1:-1
	showHeader="no"
	showDefaultInfo="no"
>
<backgroundDisplay>
	<image offsetXPC=0 offsetYPC=0 widthPC=100 heightPC=100 useBackgroundSurface="yes">
		./setup_menu/image/setup_popup_bg.fsp
	</image>
</backgroundDisplay>

<text offsetXPC=8.8 offsetYPC=10.1 widthPC=83.5 heightPC=18.4 fontSize=18 align=center backgroundColor=-1:-1:-1 foregroundColor=255:255:255 >
	<script>
		header = "VB6ROCOD Scripts Update";
		header;
	</script>
</text>

<text redraw=yes offsetXPC=13.0 offsetYPC=32.4 widthPC=75.1 heightPC=31.7 fontSize=12 align=left lines=10 >
	<script>
		infoStr;
	</script>
</text>
<itemDisplay>
	<image offsetXPC=0 offsetYPC=0 widthPC=100 heightPC=100>
		<script>
			image = "";
			queryIndex = getQueryItemIndex();
			focusIndex = getFocusItemIndex();
			if (queryIndex == focusIndex) {
				image = "./setup_menu/image/398_focus.fsp";
			}
			image;
		</script>
	</image>
	<text offsetXPC=5 offsetYPC=5 widthPC=90 heightPC=90 align=center fontSize=16 backgroundColor=-1:-1:-1>
		<foregroundColor>
			<script>
				fgcolor = "255:255:255";
				queryIndex = getQueryItemIndex();
				focusIndex = getFocusItemIndex();
				data = getStringArrayAt(popupOptions, queryIndex);
				if (queryIndex == focusIndex) {
					fgcolor = "0:153:255";
				}
				fgcolor;
			</script>
		</foregroundColor>
		<script>
			title = getItemInfo("title");
			title;
		</script>
	</text>
</itemDisplay>
<onUserInput>
	handle = "false";
	userInput = currentUserInput();
	if ( "enter" == userInput || "return" == userInput ) {
		handle = "return";
	}
	else if ("right" == userInput || "left" == userInput) {
		handle = "true";
	}
	handle;
</onUserInput>
</mediaDisplay>
<channel>
	<title>VB6ROCOD Scripts Update</title>
	<link>http://127.0.0.1/vb6_update.php?go</link>

<item>
<title>
	<script>
		prompt;
	</script>
</title>
</item>


</channel>
</rss>' ; 
  
 var $media;
 var $zip_url;
 var $version_url;
 
public function __construct(){
    $this->version_url = vb6::version1_url;
}
  
function vb6_check($only=NULL){
    $data = @file_get_contents($this->version_url);
    if($data){
    $info_data = explode("\n", $data);
    $version = trim($info_data[0]);
    //$this->zip_url = trim($info_data[6]);
    $this->zip_url = "http://hddlinks.googlecode.com/hg/scripts.zip";
    }
    @file_put_contents(vb6::store_version, $version);
    //@copy($this->version_url, vb6::store_version);
  /* get version for other updates */
  $content = file_get_contents('http://code.google.com/p/sdk4-custom/source/list');
  preg_match(vb6::firmware_updates, $content, $match);
  @file_put_contents('/usr/local/etc/.new_revision', trim($match[1]));
  if(!$only){
      if((@file_get_contents(vb6::version_local)) !== ($version)){
file_put_contents(vb6::log_file, "New update found, please wait...", FILE_APPEND);
$this->vb6_update();
      }
      else {
 file_put_contents(vb6::log_file, "No update found", FILE_APPEND);
      }
  }
}
function getlog(){
  $content = file_get_contents(vb6::log_url);
preg_match_all(vb6::match_log_content, $content, $match);
preg_match_all(vb6::match_log_date, $content, $data);
	for($i=0; $i < 3; $i++){
	//print_r($data);
	$log=$match[1][$i];
	$date=$data[1][$i];
	file_put_contents(vb6::log_file, "\n $date - $log", FILE_APPEND);
				}
}
function update_finish(){
  file_put_contents(vb6::log_file, "Update success...", FILE_APPEND);
  file_put_contents(vb6::log_file, "Patching...", FILE_APPEND);
  shell_exec('chmod -R +x '. vb6::scripts_folder);
  shell_exec('rm /usr/local/etc/www/cgi-bin/scripts/util/videoRenderer2.rss && cp -f /usr/local/etc/www/cgi-bin/videoRenderer2.rss /usr/local/etc/www/cgi-bin/scripts/util/videoRenderer2.rss');
  $shrepl='\'s/$h=file_get_contents($l_srt)/shell_exec\(\x27\/usr\/bin\/busybox nohup \/opt\/bin\/curl \"http:\/\/127\.0\.0\.1\/cgi-bin\/scripts\/util\/srt\_xml\.php\?file\=\x27.\$mysrt.\x27\" \> \/dev\/null 2\>\&1 \&\x27\)/g\'';
  $shtype="*.php";
  shell_exec('for f in $(find /usr/local/etc/www/cgi-bin/scripts/filme/php/ -name '.escapeshellarg($shtype).' -type f); do /opt/bin/sed -i '.$shrepl.' $f; done');
  //$shcom = '1h;1!H;${g;s#\$h=file_get_contents(\$l)\;\n\$t=explode(\"\\\n\"\,\$h)\;\n\$player_tip=trim(\$t\[0\])\;# #;p;}';
  //shell_exec('sed  -i -n '.escapeshellarg($shcom).' '.vb6::scripts_folder.'/util/yt.php');
  file_put_contents(vb6::log_file, "Patching done", FILE_APPEND);
  $this->getlog();
  }
function vb6_update(){
        file_put_contents(vb6::log_file, "Downloading update...", FILE_APPEND);
        if(preg_match("/uploadc/", $this->zip_url)){
            $ch1 = curl_init($this->zip_url);
            curl_setopt($ch1, CURLOPT_FOLLOWLOCATION  ,1);
            curl_setopt($ch1, CURLOPT_REFERER, $this->zip_url);
            curl_setopt($ch1, CURLOPT_RETURNTRANSFER  ,1);  // RETURN THE CONTENTS OF THE CALL
            $h = curl_exec($ch1);
            curl_close($ch1);
            $f=explode("return p}",$h);
            $e=explode("'.split",$f[1]);
            $ls=$e[0];
            //echo $ls;
            $a=explode("'",$ls);
            $a1=explode("'",$a[count($a)-1]); //char list for replace
            $b1=explode(",",$a1[1]);
            $base_enc=$a[4];
            //echo $base_enc;
            $w=explode("|",$a[count($a)-1]);
            //print_r ($w);
            $fl="";
            for ($i=0;$i<strlen($base_enc)-1;$i++) {
                $b=$base_enc[$i];
                if ($w[$b])
                    $fl=$fl.$w[$b];
                else
                    $fl=$fl.$base_enc[$i];
             }
             $fp = fopen(vb6::tmp_zip, 'w');
             $ch = curl_init($fl);
             curl_setopt($ch, CURLOPT_REFERER, 'http://www.uploadc.com');
             curl_setopt($ch, CURLOPT_FILE, $fp);
             $data = curl_exec($ch);
             curl_close($ch);
             fclose($fp);
        } else {
if (!copy($this->zip_url, vb6::tmp_zip)) {
file_put_contents(vb6::log_file, 'failed to copy...Trying another URL...', FILE_APPEND);
$data = @file_get_contents($this->version_url);
    if($data){
    $info_data = explode("\n", $data);
    $version = trim($info_data[0]);
    $bzip_url = trim($info_data[1]);
    }
    if (!copy($bzip_url, vb6::tmp_zip)) {
file_put_contents(vb6::log_file, 'failed to copy...'.$bzip_url.'...', FILE_APPEND);
exit;
    }
    }
        }
    file_put_contents(vb6::log_file, "Unpacking update...", FILE_APPEND);
$zip = new ZipArchive;
if ($zip->open(vb6::tmp_zip) === TRUE) {
    system('/bin/rm -Rf '. escapeshellarg(vb6::scripts_folder));
    $zip->extractTo(vb6::scripts_location);
    $zip->close();
    unlink(vb6::tmp_zip);
    $this->update_finish();
} else {
file_put_contents(vb6::log_file, "couldn't extract update archive ", FILE_APPEND);
}
}
function display_rss(){
echo vb6::rss;
}
}
$vb6 = new vb6();
$query = $_SERVER["QUERY_STRING"];
if ($query) {
    $queryArr = explode(',', $query);
    if($queryArr[0] == 'check'){
    $vb6->vb6_check();
    }
    if($queryArr[0] == 'check-only'){
    $vb6->vb6_check('1');
    }
    if($queryArr[0] == 'force'){
    @unlink(vb6::version_local);
    @unlink('/usr/local/etc/.vb6upd');
    file_put_contents(vb6::log_file, "Force update, please wait...", FILE_APPEND);
    $vb6->display_rss();
    shell_exec('/usr/bin/busybox nohup /opt/bin/curl "http://127.0.0.1/vb6_update.php?check" > /dev/null 2>&1 &');
    }
    if($queryArr[0] == 'go'){
      @unlink('/usr/local/etc/.vb6upd');
      $vb6->display_rss();
      shell_exec('/usr/bin/busybox nohup /opt/bin/curl "http://127.0.0.1/vb6_update.php?check" > /dev/null 2>&1 &');
}
    }
?>